﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;

namespace Common.Extensions.Dynamic
{
    public static class DynamicExtensions
    {
        public static bool DynamicViewDataDictionaryPropertyExists(dynamic dynObj, string propertyName)
        {
            if (dynObj == null) return false;
            if (String.IsNullOrEmpty(propertyName)) return false;

            DynamicObject DynDictionary;

            try
            {
                DynDictionary = dynObj as DynamicObject;
            }
            catch { return false; }

            var DynMemNames = DynDictionary.GetDynamicMemberNames();


            return (DynMemNames.Where(p => p.Equals(propertyName, StringComparison.OrdinalIgnoreCase)).Count() > 0);
        }
    }
}
